#include "ui_bofugui.h"
#include "bofugui.h"

/********************************************************************************************/

void BofuGui::on_lam4HTKButton_1_clicked()
{
    ui->statusBar->showMessage(tr("Executing lam4HTK...this will take a while!!!"),90000000);
    command = "cd "+lamFolder.absolutePath()+"/proto; make";
    commandexec = system(command.toAscii().data());
    ui->lam4HTKButton_2->setEnabled(1);
    writeToLog("echo \"HTK make done\" >> "+dir.absolutePath()+"/"+korpusname+".log");
    scriptDone();
}

void BofuGui::on_lam4HTKButton_2_clicked()
{
    ui->statusBar->showMessage(tr("Creating labelfiles...this will take a long time!!!"),90000000);
    command = "cd "+lamFolder.absolutePath()+"/proto; make .laball1";
    commandexec = system(command.toAscii().data());
    writeToLog("echo \"HTK labelfiles created\" >> "+dir.absolutePath()+"/"+korpusname+".log");
    scriptDone();
}

void BofuGui::on_lam4HTKButton_3_clicked()
{
    ui->statusBar->showMessage(tr("Executing clean"),5000);
    command = "cd "+lamFolder.absolutePath()+"/proto; make clean";
    commandexec = system(command.toAscii().data());
    ui->lam4HTKButton_2->setEnabled(0);
    writeToLog("echo \"cleaned HTK\" >> "+dir.absolutePath()+"/"+korpusname+".log");
    scriptDone();
}

void BofuGui::on_exportButton_clicked()
{
    QString targetFolder = QFileDialog::getExistingDirectory(this, tr("Choose export folder"), QDir::homePath());
    if (!targetFolder.isEmpty()){
        logDate();

        //Prepare traindata
        command = "cp "+dir.absolutePath()+"/txt/*.txt "+dir.absolutePath()+"/proto/traindata/; cp "+dir.absolutePath()+"/wav/*.wav "+dir.absolutePath()+"/proto/traindata/";
        commandexec = system(command.toAscii().data());


        ui->lineEdit_export->setText( targetFolder +"/proto ...set!!!");
        //copy files to the new folder
        command = "cp -R "+dir.absolutePath()+"/proto/ "+targetFolder+"/ >> "+dir.absolutePath()+"/"+korpusname+".log; cp -R ./.data/proto/ "+targetFolder+"/";
        commandexec = system(command.toAscii().data());
        ui->statusBar->showMessage(tr("Folder has been exported"),5000);
        lamFolder.setPath(targetFolder);
        ui->lam4HTKButton_1->setEnabled(1);
        ui->lam4HTKButton_3->setEnabled(1);

        writeToLog("echo \"Folder has been exported to: \""+targetFolder+" >> "+dir.absolutePath()+"/"+korpusname+".log");
        scriptDone();
    }
}

/********************************************************************************************/

void BofuGui::on_labelimportButton_clicked()
{
    QString sourceFolder = QFileDialog::getExistingDirectory(this, tr("Choose lab1 folder"), lamFolder.absolutePath());
    if (!sourceFolder.isEmpty()){
        logDate();
        command = "mkdir "+dir.absolutePath()+"/lab";
        QDir lab( (dir.absolutePath()+"/lab").toAscii().data() );
        if ( !lab.exists() ){

            commandexec = system(command.toAscii().data());
        } else{
            //qWarning( "Folder already exists" );
        }

        ui->lineEdit_labelfiles->setText( sourceFolder +" ...set!!!");
        QString command = "cp "+sourceFolder+"/*.lab1 "+dir.absolutePath()+"/lab1/ >> "+dir.absolutePath()+"/"+korpusname+".log";
        commandexec = system(command.toAscii().data());
        ui->statusBar->showMessage(tr("Label fles imported"),5000);
        scriptDone();
    }
}

void BofuGui::on_decodingButton_clicked()
{
    if ((!filelist.exists())||(!convert_tables.exists())){
        errorFiles();
    }else {
        logDate();
        ui->statusBar->showMessage(tr("Decoding label files...please wait!!!"),5000);
        command = "cd "+dir.absolutePath()+"; perl "+bofu.absolutePath()+"/coding_scripts_for_lam/decode.pl -list filelist.txt -conv convert_tables.txt -lab1 lab1 >> "+dir.absolutePath()+"/"+korpusname+".log 2>&1";
        commandexec = system(command.toAscii().data());
        writeToLog("echo \"label-files decoded\" >> "+dir.absolutePath()+"/"+korpusname+".log");
        scriptDone();
    }
}

/********************************************************************************************/

void BofuGui::on_labelimportButton_2_clicked()
{
    QString sourceFolder = QFileDialog::getExistingDirectory(this, tr("Choose lab folder"), QDir::homePath());
    if (!sourceFolder.isEmpty()){


        ui->lineEdit_labelfiles_2->setText( sourceFolder +" ...set!!!");
        command = "cp "+sourceFolder+"/*.lab "+dir.absolutePath()+"/lab/";
        commandexec = system(command.toAscii().data());
        ui->statusBar->showMessage(tr("Label fles have been imported"),5000);
        logDate();
        writeToLog("echo \"lab-files imported\" >> "+dir.absolutePath()+"/"+korpusname+".log");
        scriptDone();
    }
}

/********************************************************************************************/
